package com.kcwl.idea.plugins.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationUtil {
    public static final String EMAIL = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
    public static final String PHONE = "(^(\\d{2,4}[-_－—]?)?\\d{3,8}([-_－—]?\\d{3,8})?([-_－—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)";
    public static final String MOBILE = "^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$";
    public static final String SIMPLE_MOBILE = "^1[3-9]\\d{9}$";
    public static final String NUMBER = "^(\\-|\\+)?\\d+(\\.\\d+)?$";
    public static final String INTEGER = "^-?(([1-9]\\d*$)|0)";
    public static final String INTEGER_NEGATIVE = "^[1-9]\\d*|0$";
    public static final String INTEGER_POSITIVE = "^-[1-9]\\d*|0$";
    public static final String DOUBLE = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
    public static final String DOUBLE_NEGATIVE = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$";
    public static final String DOUBLE_POSITIVE = "^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$";
    public static final String AGE = "^(?:[1-9][0-9]?|1[01][0-9]|120)$";
    public static final String CODE = "[0-9]\\d{5}(?!\\d)";
    public static final String STR_ENG_NUM_ = "^\\w+$";
    public static final String STR_ENG_NUM = "^[A-Za-z0-9]+";
    public static final String STR_ENG = "^[A-Za-z]+$";
    public static final String STR_SPECIAL = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
    public static final String DATE_ALL = "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(10|12|0?[13578])([-\\/\\._]?)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(11|0?[469])([-\\/\\._]?)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(0?2)([-\\/\\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([3579][26]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$))";
    public static final String DATE_FORMAT1 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
    public static final String URL = "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$";
    public static final String IDCARD = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
    public static final String PW_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,12}$";
    public static final String SIMPLE_PWD_PATTERN = "^(?![A-Z]+$)(?![a-z]+$)(?!\\d+$)(?![\\W_]+$)\\S{8,12}$";
    public static final String SPECIAL_NUMERIC = "[ _`~!@#$%^&*+=|{}':;',\\[\\].<>/?~！@#￥%……&*——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
    public static final String NUMBER_TEXT = "^([0-9]+)$";
    public static final String ORGAN_CODE = "^[A-Z]{2,20}+$";
    public static final String UNIFIED_SOCIAL_CREDIT_CODE = "^[A-Za-z0-9]{18}+$";
    public static final String JIGOU_CODE = "^[A-Z0-9]{8}-[A-Z0-9]$";
    public static final String STR_NUM = "^[0-9]+$";
    public static final String PLATE_NUMBER = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";
    public static final String REAL_NAME = "^([\\u4e00-\\u9fa5·]{1,20}|[a-zA-Z\\.\\s]{1,20})$";
    public static final String TRAN_AMOUNT = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
    public static final String INVOICE_AMOUNT_REGEX = "^((-?[1-9]{1}\\d*)|(-?[0]{1}))(\\.(\\d){0,2})?$";
    public static final String USER_NAME = "^[\\u4e00-\\u9fa5\\da-zA-Z]{2,10}$";

    public ValidationUtil() {
    }

    public static boolean isUserName(String userName) {
        return Regular(userName, "^[\\u4e00-\\u9fa5\\da-zA-Z]{2,10}$");
    }

    public static synchronized boolean StrisNull(String str) {
        return null == str || str.trim().length() <= 0;
    }

    public static boolean StrNotNull(String str) {
        return !StrisNull(str);
    }

    public static String nulltoStr(String str) {
        return StrisNull(str) ? "" : str;
    }

    public static String nulltoStr(String str, String defaut) {
        return StrisNull(str) ? defaut : str;
    }

    public static boolean isEmail(String str) {
        return Regular(str, "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+");
    }

    public static boolean isPhone(String str) {
        return Regular(str, "(^(\\d{2,4}[-_－—]?)?\\d{3,8}([-_－—]?\\d{3,8})?([-_－—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)");
    }

    public static boolean isMobile(String str) {
        return Regular(str, "^1[3-9]\\d{9}$");
    }

    public static boolean isSimpleMobile(String str) {
        return Regular(str, "^1[3-9]\\d{9}$");
    }

    public static boolean isUrl(String str) {
        return Regular(str, "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$");
    }

    public static boolean isNumber(String str) {
        return Regular(str, "^(\\-|\\+)?\\d+(\\.\\d+)?$");
    }

    public static boolean isInteger(String str) {
        return Regular(str, "^-?(([1-9]\\d*$)|0)");
    }

    public static boolean isINTEGER_NEGATIVE(String str) {
        return Regular(str, "^[1-9]\\d*|0$");
    }

    public static boolean isINTEGER_POSITIVE(String str) {
        return Regular(str, "^-[1-9]\\d*|0$");
    }

    public static boolean isDouble(String str) {
        return Regular(str, "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$");
    }

    public static boolean isDOUBLE_NEGATIVE(String str) {
        return Regular(str, "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$");
    }

    public static boolean isDOUBLE_POSITIVE(String str) {
        return Regular(str, "^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$");
    }

    public static boolean isDate(String str) {
        return Regular(str, "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(10|12|0?[13578])([-\\/\\._]?)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(11|0?[469])([-\\/\\._]?)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(0?2)([-\\/\\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([3579][26]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([1][89][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$))");
    }

    public static boolean isDate1(String str) {
        return Regular(str, "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)");
    }

    public static boolean isAge(String str) {
        return Regular(str, "^(?:[1-9][0-9]?|1[01][0-9]|120)$");
    }

    public static boolean isLengOut(String str, int leng) {
        return StrisNull(str) ? false : str.trim().length() > leng;
    }

    public static boolean isIdCard(String str) {
        if (StrisNull(str)) {
            return false;
        } else {
            return str.trim().length() != 15 && str.trim().length() != 18 ? false : Regular(str, "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))");
        }
    }

    public static boolean isCode(String str) {
        return Regular(str, "[0-9]\\d{5}(?!\\d)");
    }

    public static boolean isEnglish(String str) {
        return Regular(str, "^[A-Za-z]+$");
    }

    public static boolean isENG_NUM(String str) {
        return Regular(str, "^[A-Za-z0-9]+");
    }

    public static boolean isENG_NUM_(String str) {
        return Regular(str, "^\\w+$");
    }

    public static String filterStr(String str) {
        Pattern p = Pattern.compile("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]");
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static boolean isJigouCode(String str) {
        return Regular(str, "^[A-Z0-9]{8}-[A-Z0-9]$");
    }

    public static boolean isSTR_NUM(String str) {
        return Regular(str, "^[0-9]+$");
    }

    private static boolean Regular(String str, String pattern) {
        if (null != str && str.trim().length() > 0) {
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(str);
            return m.matches();
        } else {
            return false;
        }
    }

    public static boolean isPlateNumber(String plateNumber) {
        return Regular(plateNumber, "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})");
    }

    public static void main(String[] args) {
        boolean plateNumber = isPlateNumber("鲁H65K00");
        System.out.println(plateNumber);
    }

    public static Boolean validatePwd(String pwd, String pattern) {
        return Regular(pwd, pattern);
    }

    public static Boolean validateName(String name) {
        return Regular(name, "^([\\u4e00-\\u9fa5·]{1,20}|[a-zA-Z\\.\\s]{1,20})$");
    }

    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[一-龥]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static Boolean validateTranAmount(String tranAmount) {
        return Regular(tranAmount, "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    }

    public static boolean validateOrganCode(String organCode) {
        return Regular(organCode, "^[A-Z]{2,20}+$");
    }

    public static Boolean validateInvoiceAmount(String tranAmount) {
        return Regular(tranAmount, "^((-?[1-9]{1}\\d*)|(-?[0]{1}))(\\.(\\d){0,2})?$");
    }

    public static Boolean specialNumeric(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        } else {
            Pattern p = Pattern.compile("[ _`~!@#$%^&*+=|{}':;',\\[\\].<>/?~！@#￥%……&*——+|{}【】‘；：”“’。，、？]|\n|\r|\t");
            Matcher m = p.matcher(str);
            return m.find();
        }
    }

}
